package com.leihao.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leihao.common.core.exception.ServiceException;
import com.leihao.common.core.utils.StringUtils;
import com.leihao.product.domian.listRequest.ProductStatsListRequest;
import com.leihao.product.domian.listRequest.ListRequest;
import com.leihao.product.domian.ProductStats;
import com.leihao.product.domian.vo.ProductStatsVo;
import com.leihao.product.mapper.ProductStatsMapper;
import com.leihao.product.service.ProductStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductStatsServiceImpl implements ProductStatsService {
    @Autowired
    private ProductStatsMapper productStatsMapper;

    @Override
    public PageInfo<ProductStatsVo> findList(ListRequest listRequest) {
        if (listRequest == null) {
            throw new ServiceException("参数异常!");
        }
        if (listRequest.getPageNum() == null || listRequest.getPageSize() == null) {
            throw new ServiceException("参数异常!");
        }
        PageHelper.startPage(listRequest.getPageNum(), listRequest.getPageSize());
        List<ProductStatsVo> list = productStatsMapper.findList();
        PageInfo<ProductStatsVo> productStatsPageInfo = new PageInfo(list);
        return productStatsPageInfo;
    }

    @Override
    public ProductStatsVo findById(Integer productStatsId) {
        if (productStatsId == null) {
            throw new ServiceException("参数异常!");
        }
        return productStatsMapper.findById(productStatsId);
    }

    @Override
    public void delete(Integer productStatsId) {
        if (productStatsId == null) {
            throw new ServiceException("参数异常!");
        }
        productStatsMapper.delete(productStatsId);
    }

    @Override
    public void update(ProductStats productStats) {
        if (StringUtils.isNull(productStats)) {
            throw new ServiceException("参数异常!");
        }
        if (productStats.getProductStatsId() == null) {
            throw new ServiceException("参数异常!");
        }
        productStatsMapper.update(productStats);
    }

    @Override
    public void add(ProductStats productStats) {
        if (StringUtils.isNull(productStats)) {
            throw new ServiceException("参数异常!");
        }
        productStatsMapper.add(productStats);
    }

    @Override
    public PageInfo<ProductStatsVo> findByAttrIdList(ProductStatsListRequest productStatsListRequest) {
        if (StringUtils.isNull(productStatsListRequest)) {
            throw new ServiceException("参数异常!参数不能为空");
        }
        if (productStatsListRequest.getProductAttrId() == null ||
                productStatsListRequest.getType() == null) {
            throw new ServiceException("参数异常!参数不能为空");
        }
        PageHelper.startPage(productStatsListRequest.getPageNum(), productStatsListRequest.getPageSize());
        List<ProductStatsVo> byAttrIdList = productStatsMapper.findByAttrIdStatsList(productStatsListRequest);
        PageInfo<ProductStatsVo> productStatsPageInfo = new PageInfo<>(byAttrIdList);
        return productStatsPageInfo;
    }

    @Override
    public List<ProductStatsVo> findByAttrIdStatsAll(Integer productAttrId, Integer type) {
        if (productAttrId == null || productAttrId <= 0) {
            throw new ServiceException("编号不存在");
        }
        //0->属性 1->参数
        if (0 != type && 1 != type){
            throw new ServiceException("请正确选择类型");
        }
        //方法复用  -> 该实体是查询列表是的分页查询对象  ->这里用到 type 和 productId 字段
        ProductStatsListRequest productStatsListRequest = new ProductStatsListRequest();
        productStatsListRequest.setProductAttrId(productAttrId);
        productStatsListRequest.setType(type);
        return productStatsMapper.findByAttrIdStatsList(productStatsListRequest);
    }

}
